package com.cloudptt.api.product.update;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.xmlpull.v1.XmlPullParser;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.models.VersionInfo;

import android.os.Handler;
import android.os.Message;
import android.util.Xml;

/**
 *@author coolszy
 *@date 2012-4-26
 *@blog http://blog.92coding.com
 */
public class ParseServerVersionInfo extends Thread
{
	private String url = null;
	private Handler mHandler = null;
	public ParseServerVersionInfo(String url,Handler mHandler)
	{
		this.url = url;
		this.mHandler = mHandler;
	}
	@Override  
	public void run() {  
		// TODO Auto-generated method stub  
		VersionInfo info = getVersionInfo(url);  
		sendMsgToUptManager(info);
	}  
	private void sendMsgToUptManager(VersionInfo info)
	{
		Message msg = mHandler.obtainMessage(UpdateManager.GetVERSION);
		msg.obj = info;
		msg.sendToTarget();
	}
	public VersionInfo getVersionInfo(String url)
	{
		String bodyStr = getServerVesion(url);
		if(bodyStr == null
				|| bodyStr.equals(""))
		{
			return null;
		}

		XmlPullParser parser = Xml.newPullParser();
		VersionInfo info = null;
		String name = null;

		try {
//			parser.setInput(new StringReader(bodyStr));
			InputStream is = new ByteArrayInputStream(bodyStr.getBytes());  
			parser.setInput(is, "utf-8");  
			int eventType = parser.getEventType();

			while (eventType != XmlPullParser.END_DOCUMENT) {

				name = parser.getName();

				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:// �ĵ���ʼ�¼�,���Խ�����ݳ�ʼ������
					info = new VersionInfo();
					break;

				case XmlPullParser.START_TAG:// ��ʼԪ���¼�

					if (name.equalsIgnoreCase("version")) {
						int versionCode = CheckData.parseInt(parser.nextText());
						info.setVersionCode(versionCode);

					} 
					else if (name.equalsIgnoreCase("name")) {
						info.setName(parser.nextText());
					} 
					else if (name.equalsIgnoreCase("url")) {
						info.setUrl(parser.nextText());
					} 
					else if (name.equalsIgnoreCase("must")) {
						info.setMust(parser.nextText());
					}
					else if (name.equalsIgnoreCase("info")) {
						info.addInfo(parser.nextText());
					}
					break;

				case XmlPullParser.END_TAG:// ����Ԫ���¼�
					name = null;
					break;
				}

				eventType = parser.next();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return info;	
	}
	private String getServerVesion(String url) {
//		BufferedReader reader =null;
//		StringBuffer sb =null;
//		String result ="";
//		HttpClient client =new DefaultHttpClient();
//		HttpGet request =new HttpGet(url);
//		request.addHeader("Content-Type"," text/html;charset=UTF-8");
//		try{
//			// �������󣬵õ���Ӧ
//			HttpResponse response = client.execute(request);
//			// �������ɹ�
//			if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//				reader =new BufferedReader(new InputStreamReader(response
//						.getEntity().getContent()));
//				sb =new StringBuffer();
//				String line ="";
//				while((line = reader.readLine()) !=null) {
//					sb.append(line);
//				}
//			}else if(response.getStatusLine().getStatusCode() == HttpStatus.SC_SERVICE_UNAVAILABLE) {
//				result ="";
//			}
//		}catch(ClientProtocolException e) {
//			e.printStackTrace();
//		}catch(Exception e) {
//			e.printStackTrace();
//		}finally{
//			try{
//				if(reader !=null) {
//					reader.close();
//					reader =null;
//				}
//			}catch(IOException e) {
//				result ="";
//				e.printStackTrace();
//			}
//		}
//		if(sb !=null) {
//			result = sb.toString();
//		}
		return "";
	}
}
